
// var mongoose = require("../config/mongoConnection");
const mongoCollections = require('../config/mongoCollections');
const books = mongoCollections.books;
class Books {
    constructor() {
        this.typeSet = {
            title: String,
            author: Object,
            genre: {
                type: Array,
                require: true
            },
            datePublished: String,
            summary: String,
            reviews: {
                type: Array,
                default: []
            }
        }
        this.validationList = {
            title: "string",
            author: "object",
            genre: "object",
            datePublished: "string",
            summary: "string",
            reviews: "object"
        }
        // var books = new mongoose.Schema(this.typeSet)
        // this.Books = mongoose.model("books", books);
    }
    async saveBooks(data) {
        try {
            const bookCollection = await books();
            const newInsertInformation = await bookCollection.insertOne(data);
            // let Books = new this.Books(data)
            // const result = await Books.save()
            return newInsertInformation

        } catch (err) {
            return err
        }

    }
    async findOne(id) {
        try {
            const bookCollection = await books();
            const newFindInformation = await bookCollection.findOne({ _id: id });
            // let result = await this.Books.findOne({ _id: id })
            return newFindInformation

        } catch (err) {
            return err
        }

    }
    async updateReviews(id, reviewsId) {
        try {
            const bookCollection = await books();
            const newFindInformation = await bookCollection.findOne({ _id: id });
            // let result = await this.Books.findOne({ _id: id })
            newFindInformation.reviews.push(reviewsId)
            return await newFindInformation.save()

        } catch (err) {
            return err
        }
    }
    async findAll(data) {
        try {
            // let result = await this.Books.find({})
            const bookCollection = await books();
            const newFindInformation = await bookCollection.find({})
            return newFindInformation

        } catch (err) {
            return err
        }

    }
    async updateOne(id, data) {
        try {
            // let result = await this.Books.findOne({ _id: id })
            // console.log(result)
            // for (let item in data) {
            //     result[item] = data[item]
            // }

            // return await result.save()


            const bookCollection = await books();
            const newFindInformation = await bookCollection.findOne({ _id: id })
            for (let item in data) {
                newFindInformation[item] = data[item]
            }

            return await newFindInformation.save()
           
        } catch (err) {
            return err
        }

    }
    async deleteOneReview(bookid, reviewid) {
        try {
            const bookCollection = await books();
            const newFindInformation = await bookCollection.findOne({ _id: bookid })
            // let books = await this.Books.findOne({ _id: bookid })
            let otherids = newFindInformation.reviews.filter(item => {
                return item != reviewid
            })
            newFindInformation.reviews = otherids

            return await newFindInformation.save()

        } catch (err) {
            return err
        }
    }
    async deleteBooks(id) {
        try {
            const bookCollection = await books();
            const newFindInformation = await bookCollection.findOne({ _id: id })
            // let books = await this.Books.findOne({ _id: id })
            if (newFindInformation) {
                let result = await bookCollection.deleteOne({ _id: id })

                return `${newFindInformation._id} is delete success`
            } else {
                throw new Error("id is not undefind")
            }

        } catch (err) {
            return err
        }
    }
    async validation(data) {
        for (let item in data) {
            for (let initem in this.validationList) {
                if (item === initem) {
                    if (typeof data[item] != this.validationList[initem]) {
                        console.log(data[item])
                        return { flag: false, error: `${item} style is error` }
                    }
                }
            }
        }

        return { flag: true }
    }
}

module.exports = new Books